//Function

export const Fps = 30;

export function changeSpeed(value) {
  return 10 / Fps * value;
}

export function Biribiri(arg) {
  navigator.vibrate = navigator.vibrate
    || navigator.webkitVibrate
    || navigator.mozVibrate
    || navigator.msVibrate;

  navigator.vibrate(arg);
}

//Class


class Element {
  constructor(arg) {
    this.x = arg.x;
    this.y = arg.y;
    this.item = arg.item;
    this.zoom = arg.zoom || 1;
    this.time = 0;
    this.jump = false;
    this.jumphigh = 10;
  }

  getX() {
    return this.x;
  }

  getY() {
    return this.y
  }

  setX(x) {
    this.x = x
  }

  setY(y) {
    this.y = y
  }

  getItem() {
    return this.item;
  }

  getPic() {
    return this.item.getPic();
  }

  getWidth() {
    return this.item.getWidth() * this.zoom;
  }

  getHeight() {
    return this.item.getHeight() * this.zoom;
  }

  bang() {
    if (this.time++ % 2 === 0) {
      this.jump = true;
      this.y -= this.jumphigh;
    } else {
      this.jump = false;
      this.y += this.jumphigh;
    }
    if (this.time === 100) this.time = 0;
  }

  getJump() {
    return this.jump;
  }
}

export class CrhFirst extends Element {
  constructor(arg) {
    super(arg);
    this.jumphigh = 1;
  }

}

export class CrhMiddle extends Element {
  constructor(arg) {
    super(arg);
    this.jumphigh = 2;
  }

}

export class CrhEnd extends Element {
}

export class Slip extends Element {
  constructor(arg) {
    super(arg);
    // this.width=arg.width;
    // this.height=arg.height;
  }

  // getHeight(){
  //   return this.height;
  // }
  // getWidth(){
  //   return this.width;
  // }
}

export class Rail extends Element {
}

export class Stone extends Element {
}

export class Sun extends Element {
}

export class Sight extends Element {
}

export class Chair extends Element {
}

export class Cloud extends Element {
  constructor(arg) {
    super(arg);
    this.alpha = Math.random();
  }

  getAlpha() {
    return this.alpha;
  }

  setAlpha() {
    this.alpha = Math.random();
  }
}

export class Build extends Element {
  constructor(arg) {
    super(arg);
    this.alpha = Math.random() * 0.2 + 0.8;
  }

  getAlpha() {
    return this.alpha;
  }
}

export class Tunnel extends Element {
}

export class Hero extends Element {
  constructor(arg) {
    super(arg);
    this.standX = arg.standX;
    this.standY = arg.standY;
    this.speed = arg.speed;
    this.speednow = 0;
    this.dir = 'down';
    this.status = 'stand';
    this.standItem = arg.standItem;
    this.walkDown = arg.walkDown;
    this.walkUp = arg.walkUp;
    this.walkLeft = arg.walkLeft;
    this.walkRight = arg.walkRight;
    this.climb = arg.climb;
    this.sitLeft = arg.sitLeft;
    this.sitRight = arg.sitRight;
    this.step = 0;
    this.dirc = 0;
    this.climbing = 0;
    this.sitting = 0;
    this.pp = 100;
  }

  getspeed() {
    return this.speednow;
  }

  setspeed(speed) {
    if (speed) {
      this.speednow = speed;
    }
    if (this.pp < 1) {
      this.pp = 1;
      this.speed = changeSpeed(0);
    } else if (this.pp < 30 && this.pp > 0) {
      this.speed = changeSpeed(15);
    } else {
      this.speed = changeSpeed(35);
    }
    switch (this.status) {
      case 'walk':
        if (this.pp > 0) this.pp -= 0.5 * (this.speednow / this.speed + 0.01);
        if (this.pp < 1) this.pp = 1;
        // console.log(this.pp);
        if (this.speednow < this.speed) {
          this.speednow += changeSpeed(1);
        } else {
          this.speednow -= changeSpeed(1);
        }
        break;
      case 'stop':
        if (this.pp > 0) this.pp -= 0.1 * this.speednow;
        if (this.pp < 1) this.pp = 1;
        if (this.speednow > 0) {
          this.speednow -= changeSpeed(2);
        } else {
          this.status = 'stand';
        }
        break;
    }

  }

  getstandX() {
    return this.standX;
  }

  getstandY() {
    return this.standY;
  }

  setstandX(x) {
    this.standX = x;
  }

  setstandY(y) {
    this.standY = y;
  }

  getStand() {
    return this.standItem
  }

  getWalk(dirc) {
    switch (dirc) {
      case 0:
        return this.walkLeft;
        break;
      case 1:
        return this.walkRight;
        break;
      // case 2:
      //   return this.walkDown;
      //     break;
      // case 3:
      //   return this.walkUp;
      //     break;
    }
  }

  getClimb() {
    return this.climb
  }

  getSit() {
    if (this.dirc === 1) {
      return this.sitLeft;
    } else {
      return this.sitRight;
    }
  }

  addStep() {
    if (this.step > 29) this.step = 3;
    return Math.floor((this.step++) / 3);
  }

  subClimbing() {
    if (this.climbing < 0) {
      this.climbing = 0;
      this.status = 'stand';
    }
    this.standY = -this.climbing * 5;
    return Math.floor(this.climbing-- / 3);
  }

  addClimbing() {
    if (this.climbing > 16) {
      if (this.pp < 100) this.pp++;
      this.climbing = 16;
    }
    this.standY = -this.climbing * 5;
    return Math.floor(this.climbing++ / 3);
  }

  addSitting() {
    if (this.sitting > 7) {
      this.sitting = 7;
      if (this.pp < 100) this.pp += 1.5;
      else if (this.pp > 100) this.pp = 100;
    }
    this.standY = this.sitting * 2;
    return Math.floor(this.sitting++ / 3);
  }

  subSitting() {
    if (this.sitting < 0) {
      this.sitting = 0;
      this.status = 'stand';

    }
    this.standY = this.sitting * 2;
    return Math.floor(this.sitting-- / 3);
  }

  paint(_this) {
    if (this.speednow > 0 && (this.status === 'walk' || this.status === 'stop')) {
      if (this.dirc === 0) {
        this.standX -= this.speednow;
        if (this.standX < -_this.width / 2) this.standX = -_this.width / 2;
      } else {
        this.standX += this.speednow;
        let crh = _this.crh_middle[_this.crh_middle.length - 1];
        if (this.standX > crh.getWidth() + crh.getX() - 180) {
          this.standX = crh.getWidth() + crh.getX() - 180
        }
      }
    }
    switch (this.dir) {
      case 'right':
        this.dirc = 1;
        break;
      case 'left':
        this.dirc = 0;
        break;
      case 'down':
        // this.dirc=2;
        break;
      case 'up':
        // this.dirc=3;
        break;
    }
    let tWidth = this.getStand().getWidth() / 2 * this.zoom;
    let tHeight = this.getStand().getHeight() * this.zoom;
    switch (this.status) {
      case 'walk':
      case 'stop':
        tWidth = this.getWalk(this.dirc).getWidth() / 10 * this.zoom;
        tHeight = this.getWalk(this.dirc).getHeight() * this.zoom;
        _this.ctx.drawImage(this.getWalk(this.dirc).getPic(),
          this.addStep() * this.getWalk(this.dirc).getWidth() / 10,
          0,
          this.getWalk(this.dirc).getWidth() / 10,
          this.getWalk(this.dirc).getHeight(),
          this.x - tWidth / 2,
          this.y - tHeight,
          tWidth,
          tHeight
        );
        break;
      case 'climbing':
        tWidth = this.getClimb().getWidth() / 6 * this.zoom;
        tHeight = this.getClimb().getHeight() * this.zoom;
        _this.ctx.drawImage(this.getClimb().getPic(),
          this.addClimbing() * this.getClimb().getWidth() / 6,
          0,
          this.getClimb().getWidth() / 6,
          this.getClimb().getHeight(),
          this.x - tWidth / 2,
          this.y - tHeight,
          tWidth,
          tHeight
        );
        break;
      case 'drop':
        tWidth = this.getClimb().getWidth() / 6 * this.zoom;
        tHeight = this.getClimb().getHeight() * this.zoom;
        _this.ctx.drawImage(this.getClimb().getPic(),
          this.subClimbing() * this.getClimb().getWidth() / 6,
          0,
          this.getClimb().getWidth() / 6,
          this.getClimb().getHeight(),
          this.x - tWidth / 2,
          this.y - tHeight,
          tWidth,
          tHeight
        );
        break;
      case 'sitdown':
        tWidth = this.getSit().getWidth() / 3 * this.zoom;
        tHeight = this.getSit().getHeight() * this.zoom;
        _this.ctx.drawImage(this.getSit().getPic(),
          this.addSitting() * this.getSit().getWidth() / 3,
          0,
          this.getSit().getWidth() / 3,
          this.getSit().getHeight(),
          this.x - tWidth / 2,
          this.y - tHeight,
          tWidth,
          tHeight
        );
        break;
      case 'getup':
        tWidth = this.getSit().getWidth() / 3 * this.zoom;
        tHeight = this.getSit().getHeight() * this.zoom;
        _this.ctx.drawImage(this.getSit().getPic(),
          this.subSitting() * this.getSit().getWidth() / 3,
          0,
          this.getSit().getWidth() / 3,
          this.getSit().getHeight(),
          this.x - tWidth / 2,
          this.y - tHeight,
          tWidth,
          tHeight
        );
        break;
      case 'stand':
      default:
        // if(this.pp<1) this.pp=1;
        if (this.pp < 100) this.pp += 0.2;
        _this.ctx.drawImage(this.standItem.getPic(),
          this.dirc * this.getStand().getWidth() / 2,
          0,
          this.getStand().getWidth() / 2,
          this.getStand().getHeight(),
          this.x - tWidth / 2,
          this.y - tHeight,
          tWidth,
          tHeight
        );
    }

  }
}

export class Character extends Hero {
  constructor(arg) {
    super(arg);
  }

  setSit(arg) {
    this.zoom = arg.zoom;
    this.standX = arg.standX;
    this.standY = arg.standY;
    this.speed = arg.speed;
    this.standItem = arg.standItem;
    this.walkRight = arg.walkRight;
    this.walkLeft = arg.walkLeft;
    this.climb = arg.climb;
    this.status = arg.status;
    this.dir = arg.dir;
    this.climbing = arg.climbing;
    this.sitting = arg.sitting;
    this.sitLeft = arg.sitLeft;
    this.sitRight = arg.sitRight;
  }

  setspeed(speed) {
    if (speed) {
      this.speednow = speed;
    }
    switch (this.status) {
      case 'walk':
        if (this.pp > 0) this.pp--;
        if (this.speednow < this.speed) {
          this.speednow += changeSpeed(1);
        } else {
          this.speednow -= changeSpeed(1);
        }
        break;
      case 'stop':
        if (this.speednow > 0) {
          this.speednow -= changeSpeed(2);
        } else {
          this.status = 'stand';
        }
        break;
    }
  }

  paint(_this) {
    if (this.speednow > 0 && (this.status === 'walk' || this.status === 'stop')) {
      if (this.dirc === 0) {
        this.standX -= this.speednow;
      } else {
        this.standX += this.speednow;
      }
    }
    switch (this.dir) {
      case 'right':
        this.dirc = 1;
        break;
      case 'left':
        this.dirc = 0;
        break;
      case 'down':
        // this.dirc=2;
        break;
      case 'up':
        // this.dirc=3;
        break;
    }
    let tWidth = this.getStand().getWidth() / 2 * this.zoom;
    let tHeight = this.getStand().getHeight() * this.zoom;
    switch (this.status) {
      case 'walk':
      case 'stop':
        tWidth = this.getWalk(this.dirc).getWidth() / 10 * this.zoom;
        tHeight = this.getWalk(this.dirc).getHeight() * this.zoom;
        _this.ctx.drawImage(this.getWalk(this.dirc).getPic(),
          this.addStep() * this.getWalk(this.dirc).getWidth() / 10,
          0,
          this.getWalk(this.dirc).getWidth() / 10,
          this.getWalk(this.dirc).getHeight(),
          this.standX - _this.hero.getstandX() - tWidth / 2 + _this.hero.x,
          _this.hero.getY() - tHeight,
          tWidth,
          tHeight
        );
        break;
      case 'climbing':
        if (_this.hero.climbing > 8 || this.climbing < 15) {
          tWidth = this.getClimb().getWidth() / 6 * this.zoom;
          tHeight = this.getClimb().getHeight() * this.zoom;
          _this.ctx.drawImage(this.getClimb().getPic(),
            this.addClimbing() * this.getClimb().getWidth() / 6,
            0,
            this.getClimb().getWidth() / 6,
            this.getClimb().getHeight(),
            this.standX - _this.hero.getstandX() - tWidth / 2 + _this.hero.x,
            _this.hero.getY() - tHeight,
            tWidth,
            tHeight
          );
        }
        // else if(_this.hero.status!=='climbing'&&_this.hero.climbing<15&&this.climbing>14) {       }
        break;
      case 'drop':
        tWidth = this.getClimb().getWidth() / 6 * this.zoom;
        tHeight = this.getClimb().getHeight() * this.zoom;
        _this.ctx.drawImage(this.getClimb().getPic(),
          this.subClimbing() * this.getClimb().getWidth() / 6,
          0,
          this.getClimb().getWidth() / 6,
          this.getClimb().getHeight(),
          this.standX - _this.hero.getstandX() - tWidth / 2 + _this.hero.x,
          _this.hero.getY() - tHeight,
          tWidth,
          tHeight
        );
        break;
      case 'sitdown':
        tWidth = this.getSit().getWidth() / 3 * this.zoom;
        tHeight = this.getSit().getHeight() * this.zoom;
        _this.ctx.drawImage(this.getSit().getPic(),
          this.addSitting() * this.getSit().getWidth() / 3,
          0,
          this.getSit().getWidth() / 3,
          this.getSit().getHeight(),
          this.standX - _this.hero.getstandX() - tWidth / 2 + _this.hero.x,
          _this.hero.getY() - tHeight,
          tWidth,
          tHeight
        );
        break;
      case 'getup':
        tWidth = this.getSit().getWidth() / 3 * this.zoom;
        tHeight = this.getSit().getHeight() * this.zoom;
        _this.ctx.drawImage(this.getSit().getPic(),
          this.subSitting() * this.getSit().getWidth() / 3,
          0,
          this.getSit().getWidth() / 3,
          this.getSit().getHeight(),
          this.standX - _this.hero.getstandX() - tWidth / 2 + _this.hero.x,
          _this.hero.getY() - tHeight,
          tWidth,
          tHeight
        );
        break;
      case 'stand':
      default:
        // console.log(this.standX);
        // console.log(this.standX-_this.hero.getstandX());
        _this.ctx.drawImage(this.standItem.getPic(),
          this.dirc * this.getStand().getWidth() / 2,
          0,
          this.getStand().getWidth() / 2,
          this.getStand().getHeight(),
          this.standX - _this.hero.getstandX() - tWidth / 2 + _this.hero.x,
          _this.hero.getY() - tHeight,
          tWidth,
          tHeight
        );
    }
  }
}

export class Box {
  constructor(alpha = 0) {
    this.alpha = alpha;
  }

  addAlpha(value) {
    this.alpha += value;
  }

  subAlpha(value) {
    this.alpha -= value;
  }

  getAlpha() {
    return this.alpha
  }

  paint(_this) {
    _this.ctx.globalAlpha = this.alpha;
    _this.ctx.beginPath();
    _this.ctx.moveTo(0, 0);
    _this.ctx.lineTo(_this.width, 0);
    _this.ctx.lineTo(_this.width, _this.height);
    _this.ctx.lineTo(0, _this.height);
    _this.ctx.lineTo(0, 0);     //绘制最后一笔使图像闭合
    _this.ctx.fillStyle = "black";
    _this.ctx.fill();
    _this.ctx.stroke();
  }
}

export class Triangle {
  constructor(arg) {
    this.x = arg.x;
    this.y = arg.y;
    this.a = arg.a;
    this.color = arg.color;
  }

  setPos(arg) {
    this.x = arg.x;
    this.y = arg.y;
    this.a = arg.a;
    this.color = arg.color;
  }

  paint(_this) {
    // _this.ctx.globalAlpha=this.alpha;
    _this.ctx.beginPath();
    _this.ctx.moveTo(this.x - this.a / 2, this.y - this.a / 3);
    _this.ctx.lineTo(this.x + this.a / 2, this.y - this.a / 3);
    _this.ctx.lineTo(this.x, this.y + this.a * 2 / 3);
    _this.ctx.lineTo(this.x - this.a / 2, this.y - this.a / 3);     //绘制最后一笔使图像闭合
    _this.ctx.fillStyle = this.color;
    _this.ctx.fill();
    _this.ctx.stroke();
  }
}

export class Bar {
  constructor(arg) {
    this.x = arg.x;
    this.y = arg.y;
    this.a = arg.a;
    this.b = arg.b;
  }

  setWidth(a) {
    this.a = a;
  }

  paint(_this) {
    _this.ctx.globalAlpha = this.alpha;
    _this.ctx.beginPath();
    _this.ctx.moveTo(this.x, this.y);
    _this.ctx.lineTo(this.x, this.y + this.b);
    _this.ctx.lineTo(this.x + this.a, this.y + this.b);
    _this.ctx.lineTo(this.x + this.a, this.y);
    _this.ctx.lineTo(this.x, this.y);     //绘制最后一笔使图像闭合
    _this.ctx.fillStyle = "white";
    _this.ctx.fill();
    _this.ctx.stroke();
  }
}
